import {
  delay, call, put, all, takeLatest
} from 'redux-saga/effects';
import api from '@API'
import {
  ADD_OPERATION
} from '@ACTIONS/counter';

const addOperation = function* addOperation() {
  try {
    console.log('SAGA', api)
    const code = yield call(api.loginWithWeXinData)
    console.log('code', code)
    yield put({ type: ADD_OPERATION.SUCCESS })
  } catch (error) {
    yield put({ type: ADD_OPERATION.FAILURE })
  }
}
const syncAddOperation = function* syncAddOperation(timeout) {
  try {
    yield delay(timeout)
    console.log('SAGA sync', timeout)
    // const code = yield call(api.getCode)
    // console.log('code', code)
    yield put({ type: ADD_OPERATION.SUCCESS })
  } catch (error) {
    yield put({ type: ADD_OPERATION.FAILURE })
  }
}

export default function* rootSaga() {
  yield all([
    takeLatest(ADD_OPERATION.REQUEST, addOperation),
    takeLatest('ASYNC_ADD', syncAddOperation),
  ]);

  // yield all([
  //   fork(watchAddOperation)
  // ])
}
